﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//以上几句是自动生成的

using System.IO.Ports;//我加的，包含串口相关的
using System.Text.RegularExpressions;//我加的


namespace tempsensor
{
    public partial class Form1 : Form
    {
        private SerialPort comm = new SerialPort();//我加的，新建一个串口变量
        private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建，定义到外面。  
        bool shoukong_flag = false;//定义手动控制标志位
        byte[] SendBuf = new byte[2];//定义串口发送字符串变量

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPortName.Items.AddRange(ports);
            comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;

            //初始化SerialPort对象  
            comm.NewLine = "/r/n";
            comm.RtsEnable = true;//根据实际情况吧。 
            comm.DataReceived += comm_DataReceived;

            jiedian1_jidianqi_kai.Enabled = false;//继电器1开按钮设置为不可用
            jiedian1_jidianqi_guan.Enabled = false;//继电器1关按钮设置为不可用
            
        }

        private void comboPortName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonOpenCloseCom_Click(object sender, EventArgs e)
        {
            //根据当前串口对象，来判断操作  
            if (comm.IsOpen)
            {
                comm.Close();
            }
            else
            {
                //关闭时点击，则设置好端口，波特率后打开  
                comm.PortName = comboPortName.Text;
                comm.BaudRate = 38400;
                try
                {
                    comm.Open();
                    
                }
                catch (Exception ex)
                {
                    //捕获到异常信息，创建一个新的comm对象，之前的不能用了。  
                    comm = new SerialPort();
                    //现实异常信息给客户。  
                    MessageBox.Show(ex.Message);
                }
            }
            //设置按钮的状态  
            buttonOpenCloseCom.Text = comm.IsOpen ? "关闭串口" : "打开串口";
        }

        void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int n = comm.BytesToRead;//先记录下来，避免某种原因，人为的原因，操作几次之间时间长，缓存不一致
            byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据
            comm.Read(buf, 0, n);//读取缓冲数据
            builder.Clear();//清除字符串构造器的内容
            //因为要访问ui资源，所以需要使用invoke方式同步ui。
            this.BeginInvoke((EventHandler)(delegate
            {
                
                
                    if (buf[0] == 'w')
                    {
                        int wenDuZhi = (buf[1] - 48) * 10 + (buf[2] - 48);
                        wendubj.BackColor = Color.GreenYellow;//则保持绿色不变
                        this.WenDuZhi_1.Text = wenDuZhi.ToString();
                        if (wenDuZhi > Convert.ToInt32(this.wenduyuzhi.Text))
                        {
                            wendubj.BackColor = Color.Red;//就变为红色
                            wendusheng.URL = "E:\\Edge\\Justin Bieber - Peaches.flac";
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x31;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(100);
                        }
                        else
                        {
                            wendubj.BackColor = Color.GreenYellow;//则保持绿色不变
                            wendusheng.URL = "";
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x30;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(100);
                        }
                        // 读取并计算湿度
                        int Shiduzhi = (buf[4] - 48) * 10 + (buf[5] - 48);
                        shidubj.BackColor = Color.GreenYellow;//则保持绿色不变
                        this.shiduzhi.Text = Shiduzhi.ToString();
                        if (Shiduzhi > Convert.ToInt32(this.shiduyuzhi.Text))
                        {
                            shidubj.BackColor = Color.Red;//就变为红色
                            shidusheng.URL = "E:\\Edge\\Justin Bieber - Peaches.flac";
                        }
                    if (shoukong_flag == false)
                    {
                        if (buf[1] > 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x31;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }

                        if (buf[1] <= 50)
                        {
                            SendBuf[0] = 0x31;
                            SendBuf[1] = 0x30;
                            comm.Write(SendBuf, 0, 2);
                            System.Threading.Thread.Sleep(200);
                        }

                    }


                }
                    if (buf[9] == 'y')
                    {
                        int yanwu = (buf[11] - 48) * 10 + (buf[12] - 48);
                        yanwubj.BackColor = Color.GreenYellow;//则保持绿色不变
                        this.yanwu1.Text = yanwu.ToString();
                        if (yanwu > Convert.ToInt32(this.yanwuyuzhi.Text))
                        {
                            yanwubj.BackColor = Color.Red;//就变为红色
                            yanwusheng.URL = "E:\\Edge\\Justin Bieber - Peaches.flac";

                        }

                    }
                    if (buf[6] == 'g')
                    {
                        int guangqiang = (buf[7] - 48) * 10 + (buf[8] - 48);
                        guangqiangbj.BackColor = Color.GreenYellow;//则保持绿色不变
                        this.tB_jiedian1_guangqiang.Text = guangqiang.ToString();
                        if (guangqiang > Convert.ToInt32(this.guangqiangyuzhi.Text))
                        {
                            guangqiangbj.BackColor = Color.Red;//就变为红色
                            guangqiangsheng.URL = "E:\\Edge\\Justin Bieber - Peaches.flac";
                        }
                        
                    }
                    
                    if (buf[13] == 'r')
                    {
                        this.renti1.Text = (buf[14] - 48).ToString() ;//显示在节点1光强文本框内
                        if (buf[14] == '0')//如果串口接收到0，
                        {
                            daozei.BackColor = Color.GreenYellow;//则保持绿色不变
                        }
                        else//否则有人在人体红外传感器周围活动
                        {
                            daozei.BackColor = Color.Red;//就变为红色
                            daozeisheng.URL = "E:\\Edge\\Justin Bieber - Peaches.flac";
                        }
                    }
                   
                    
                


            }));

        }

        private void WenDuZhi_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void jiedian1_jidianqi_kai_Click(object sender, EventArgs e)
        {
            //byte[] SendBuf = new byte[2];
            //SendBuf[0] = 0x30;
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x31;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(100);
        }

        private void jiedian1_jidianqi_guan_Click(object sender, EventArgs e)
        {
            //byte[] SendBuf = new byte[2];
            //SendBuf[0] = 0x30;
            SendBuf[0] = 0x31;
            SendBuf[1] = 0x30;
            comm.Write(SendBuf, 0, 2);
            System.Threading.Thread.Sleep(100);
        }

        private void zidong_kongzhi_CheckedChanged(object sender, EventArgs e)
        {
            if (zidong_kongzhi.Checked)
            {
                //zidong_kongzhi.Text = "手动控制";//被选中的时候，显示动控制，说明节点的继电器有节点自己控制
                //SendBuf[0] = 0x30;
                //SendBuf[1] = 0x30;
                //SendBuf[2] = 0x30;
                //comm.Write(SendBuf, 0, 3);
                //System.Threading.Thread.Sleep(100);
                shoukong_flag = true;
                jiedian1_jidianqi_kai.Enabled = true;//继电器1开按钮设置为可用
                jiedian1_jidianqi_guan.Enabled = true;//继电器1关按钮设置为可用
                
            }
            else
            {
                //zidong_kongzhi.Text = "自动控制";
                //SendBuf[0] = 0x31;
                //SendBuf[1] = 0x30;
                //SendBuf[2] = 0x30;
                //comm.Write(SendBuf, 0, 1);
                //System.Threading.Thread.Sleep(100);
                shoukong_flag = false;
                jiedian1_jidianqi_kai.Enabled = false;//继电器1开按钮设置为不可用
                jiedian1_jidianqi_guan.Enabled = false;//继电器1关按钮设置为不可用
                
            }
        }

        private void XuanZeChuanKou_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void wenduyuzhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void shiduyuzhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void comcaozuo_Enter(object sender, EventArgs e)
        {

        }
    }
}
